package com.gmj.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gmj.entity.Admin;
import com.gmj.entity.Student;
import com.gmj.entity.Teacher;
import com.gmj.service.IAdminService;
import com.gmj.service.IStudentService;
import com.gmj.service.ITeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
public class UserController {
    @Autowired
    private IAdminService as;
    @Autowired
    private IStudentService ss;
    @Autowired
    private ITeacherService ts;
    @GetMapping("/login/{username}/{password}/{role}")
    public boolean login(@PathVariable("username") String username,
                         @PathVariable("password") String password,
                         @PathVariable("role") int role,
                         HttpSession session){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("name",username);
        wrapper.eq("pwd",password);
        Object one=null;
        if(role==1){
            one = ss.getOne(wrapper);
            session.setAttribute("user",one);
        }
        if (role==2){
            one = ts.getOne(wrapper);
            session.setAttribute("user",one);
        }
        if(role==3){
            one = as.getOne(wrapper);
            session.setAttribute("user",one);
        }
        return one!=null;
    }
}
